using System;
using System.Xml;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SkyBurner
{
    public class HiscoreItem
    {
        private int m_nScore;
        private String m_Name;

        public int Score
        {
            get { return m_nScore; }
        }

        public String Name
        {
            get { return m_Name; }
        }

        public HiscoreItem(int pScore, String pName)
        {
            m_nScore = pScore;
            m_Name = pName;
        }
    };

    public class HiscoreTable
    {
        private Collection<HiscoreItem> m_hiscoreTable = new Collection<HiscoreItem>();
        static private HiscoreTable m_HiscoreTableInstance = null;

        public int ItemCount
        {
            get { return m_hiscoreTable.Count; }
        }

        protected HiscoreTable()
        {

        }

        public static HiscoreTable Instance()
        {
            if (m_HiscoreTableInstance == null)
                m_HiscoreTableInstance = new HiscoreTable();
            return m_HiscoreTableInstance;
        }

        public void Load(String pHiscoreXMLFile)
        {            
            XmlDocument xmlHiscore = new XmlDocument();

            try
            {
                xmlHiscore.Load(pHiscoreXMLFile);

                XmlNode xmlRootNode = xmlHiscore.DocumentElement;
                foreach (XmlNode scoreNode in xmlRootNode.ChildNodes)
                    m_hiscoreTable.Add(new HiscoreItem(Convert.ToInt32(scoreNode.ChildNodes[0].InnerText), scoreNode.ChildNodes[1].InnerText));
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Save(String pHiscoreXMLFile)
        {
            XmlDocument xmlHiscore = new XmlDocument();

            try
            {
                XmlNode rootNode = xmlHiscore.CreateNode(XmlNodeType.Element, "Hiscores", "");
                xmlHiscore.AppendChild(rootNode);

                foreach (HiscoreItem hsItem in m_hiscoreTable)
                {
                    XmlNode entryNode = xmlHiscore.CreateNode(XmlNodeType.Element, "Entry", "");
                    
                    XmlNode entryScoreNode = xmlHiscore.CreateNode(XmlNodeType.Element, "Score", "");
                    entryScoreNode.InnerText = Convert.ToString(hsItem.Score);

                    XmlNode entryNameNode = xmlHiscore.CreateNode(XmlNodeType.Element, "Name", "");
                    entryNameNode.InnerText = hsItem.Name;

                    entryNode.AppendChild(entryScoreNode);
                    entryNode.AppendChild(entryNameNode);

                    rootNode.AppendChild(entryNode);
                }
                xmlHiscore.Save(pHiscoreXMLFile);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public HiscoreItem GetAt(int pPosition)
        {
            return m_hiscoreTable[pPosition];
        }

        public void AddScore(int pScore, String pName)
        {
            for (int pos = 0; pos < m_hiscoreTable.Count; pos++)
            {
                if (pScore >= m_hiscoreTable[pos].Score)
                {
                    m_hiscoreTable.Insert(pos, new HiscoreItem(pScore, pName));
                    m_hiscoreTable.RemoveAt(m_hiscoreTable.Count - 1);
                    break;
                }                
            }
        }

        public bool IsHiscore(int pScore)
        {
            return (pScore >= m_hiscoreTable[m_hiscoreTable.Count - 1].Score);
        }
    }
}
